package servlet;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

@WebFilter(filterName = "ModifyPwdFilter", value = {"/modify.html"})
public class ModifyPwdFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpSession session = (httpServletRequest).getSession();
        String email = (String) session.getAttribute("email");
        if(email == null || email.trim().isEmpty()){
            // 未输入邮箱
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            // 获取应用上下文对象
            ServletContext servletContext = httpServletRequest.getServletContext();
            String contextPath = servletContext.getContextPath();
            System.out.println(contextPath);
            String url = contextPath + "/email.jsp";
            httpServletResponse.sendRedirect(url);
            return;
        }

        chain.doFilter(request, response);
    }
}
